#include "led.h"
#include "FreeRTOS.h"
#include "task.h"
#include "./lcd/bsp_ili9341_lcd.h"
#include "usart.h"
#include "key.h"
#include "event_groups.h"
#include "stdio.h"

static TaskHandle_t AppTaskCreate_Handle = NULL;
static TaskHandle_t led_task_handle = NULL;
static TaskHandle_t key_task_handle = NULL;

static EventGroupHandle_t Event_Handle =NULL;

#define KEY1_EVENT  (0x01 << 0)
#define KEY2_EVENT  (0x01 << 1)


static void AppTaskCreate(void);

static void LED_Task(void* pvParameters);
static void KEY_Task(void* pvParameters);

int main(void)
{ 
	BaseType_t xReturn = pdPASS;
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组4
	KEY_Init();
	LED_Init();		        //初始化LED端口
	ILI9341_Init();//液晶初始化    
	LCD_SetColors(BLACK,WHITE);//设置白底黑字
	LCD_SetFont(&Font8x16);//设置字体大小
	ILI9341_Clear(0,0,LCD_X_LENGTH,LCD_Y_LENGTH);//清屏
  ILI9341_GramScan ( 6 );//设置显示模式
	USART_Config(115200);
  LED_Color(LED_OFF);
	
	xReturn = xTaskCreate((TaskFunction_t )AppTaskCreate,  
                        (const char*    )"AppTaskCreate",
                        (uint16_t       )512, 
                        (void*          )NULL,
                        (UBaseType_t    )1, 
                        (TaskHandle_t*  )&AppTaskCreate_Handle);
	if(pdPASS == xReturn){
		vTaskStartScheduler();          //开启任务调度
	}
  else{
		printf("Error Start RTOS\n");
	}
	
}


static void AppTaskCreate(void)
{
  BaseType_t xReturn = pdPASS;
  
  taskENTER_CRITICAL();   
	
  Event_Handle = xEventGroupCreate();	 
  if(NULL != Event_Handle)
    printf("Success Create Event_Handle\r\n");
    
  xReturn = xTaskCreate((TaskFunction_t )LED_Task, 
                        (const char*    )"LED_Task",
                        (uint16_t       )512,   
                        (void*          )NULL,	
                        (UBaseType_t    )2,	    
                        (TaskHandle_t*  )&led_task_handle);
  if(pdPASS == xReturn)
    printf("Success Create LED_Task\r\n");
  

  xReturn = xTaskCreate((TaskFunction_t )KEY_Task,  
                        (const char*    )"KEY_Task",
                        (uint16_t       )512,
                        (void*          )NULL,
                        (UBaseType_t    )3, 
                        (TaskHandle_t*  )&key_task_handle);
  if(pdPASS == xReturn)
    printf("Success Create KEY_Task\r\n");
  
  vTaskDelete(AppTaskCreate_Handle);
  
  taskEXIT_CRITICAL();          
}




static void LED_Task(void* parameter)
{	
  EventBits_t r_event;  
  while (1)
	{
    r_event = xEventGroupWaitBits(Event_Handle,  
                                  KEY1_EVENT|KEY2_EVENT,
                                  pdTRUE,   
                                  pdTRUE, 
                                  portMAX_DELAY);
                        
    if((r_event & (KEY1_EVENT|KEY2_EVENT)) == (KEY1_EVENT|KEY2_EVENT)) 
    {
      printf ( "KEY1 And KEY2 All pressed\n");		
    }
    else
      printf ( "ERROR\n");	
  }
}


static void KEY_Task(void* parameter)
{	 
 
  while (1)
  {
    if(GET_KEY1 )       
		{
      printf ( "KEY111111111\n" );
			xEventGroupSetBits(Event_Handle,KEY1_EVENT);  					
		}
    
		if( GET_KEY2)       
		{
      printf ( "KEY222222222\n" );	
			xEventGroupSetBits(Event_Handle,KEY2_EVENT); 				
		}
		vTaskDelay(20);    
  }
}

